package com.huli.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huli.dao.CarMapper;
import com.huli.domain.Car;
import com.huli.domain.User;
import com.huli.exception.CustomException;
import com.huli.param.UserNewCarParam;
import com.huli.service.CarService;
import com.huli.service.UserService;
import com.huli.utils.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CarServiceImpl implements CarService {

    @Autowired
    CarMapper carMapper;

    @Autowired
    UserService userService;

    @Override
    public Car getCar(int carId) {
        Car car = carMapper.selectById(carId);
        if (car == null) throw new CustomException(Result.failedResult(500, "车辆不存在"));
        return car;
    }

    @Override
    public void newCar(UserNewCarParam param) {
        Car car = new Car();
        BeanUtils.copyProperties(param, car);
        car.setCarCheck(0);
        carMapper.insert(car);
    }

    @Override
    public List<Car> getCarList(int userId, int carCheck) {
        return carMapper.selectList(new QueryWrapper<Car>().eq("user_id", userId).eq("car_check", carCheck));
    }

    @Override
    public User getUserByCarId(int carId){
        Car car = getCar(carId);
        return userService.getUser(car.getUserId());
    }
}
